package com.baozun.houji.goods.viewmodel.state

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.model.bean.ApiPagerResponse
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.widget.loading.CustomPageLoadView
import com.baozun.houji.goods.network.homeApiService
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：SearchResultViewModel
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/10
 * @Version：1.0.0
 */
class SearchResultViewModel : BaseViewModel() {
    var productList = mutableListOf<GoodsInfoBean>()
    var mGoodsList = MutableLiveData<ApiPagerResponse<List<GoodsInfoBean>>>()
    var mPage = 1

    /**
     * search_type:1:淘宝/天猫 2:京东 3:自营
     * sort_order:升降类型 2:降序 非2为升序
     */
    fun search(params: HashMap<String, String>, refreshLayout: SmartRefreshLayout, loadView: CustomPageLoadView) {
        if (params["search_item"].isNullOrEmpty()) {
            appContext.showToast("搜索内容不能为空")
            return
        }
        request({ homeApiService.search(params) }, {
            mGoodsList.value = it
            mPage += 1
        }, {
            refreshLayout.finishRefresh()
            loadView.hidePlaceholderView()
        })
    }
}